package com.developer.platform.application.query.assembler;

import cn.hutool.core.bean.BeanUtil;
import com.developer.platform.application.query.dto.MenuListDto;
import com.developer.platform.application.query.dto.MenuParentDto;
import com.developer.platform.application.query.dto.MenuTreeDto;
import com.developer.platform.application.query.dto.RoleMenuDto;
import com.developer.platform.domain.Menu;
import com.veeker.core.utils.TreeUtil;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2020-11-09
 */
@Component
public class MenuAssembler {

    public List<MenuListDto> toMenuListDtoPage(List<Menu> list){
        return TreeUtil.buildByRecursive(list.stream().map(menu -> {
            MenuListDto menuListDto = new MenuListDto(menu.getId(), menu.getParentId());
            BeanUtil.copyProperties(menu, menuListDto);
            return menuListDto;
        }).collect(Collectors.toList()),0L);
    }

    public List<MenuParentDto> toMenuParentDto(List<Menu> list){
        return list.stream().map(l -> new MenuParentDto(l.getId(), l.getTitle())).collect(Collectors.toList());
    }

    public List<MenuTreeDto> toMenuTreeDto(List<Menu> list){
        return TreeUtil.buildByRecursive(list.stream()
                .map(m -> new MenuTreeDto(m.getId(), m.getParentId(), m.getTitle()))
                .collect(Collectors.toList()), 0L);
    }


    public RoleMenuDto toRoleMenuDto(List<MenuTreeDto> treeDtos, List<Long> menuIds){
        return new RoleMenuDto(treeDtos, menuIds);
    }

}
